<?php

/**
 * some generic table
 *
 * @author     Mon Zafra <monzee at gmail>
 * @copyright  (c)2009 Mon Zafra
 * @category   Mz
 * @package    modules
 * @subpackage crud
 * @license    http://mz-project.googlecode.com/svn/trunk/LICENSE    MIT License
 * @version    SVN: $Id: Table.php 12 2009-02-28 13:39:31Z monzee $
 */
class Crud_Table extends Zend_Db_Table_Abstract
{
    /**
     * Set table
     *
     * @param string $table Table name
     * @return Crud_Table
     */
    public function setTableName($table)
    {
        $this->_name = $table;
        return $this;
    }

    public function getTableName()
    {
        return $this->_name;
    }

    public function getColumns()
    {
        return $this->info(self::COLS);
    }

    public function getPrimaryKeys()
    {
        return $this->info(self::PRIMARY);
    }

    /**
     * Get single row by primary key(s)
     *
     * @param array $params Array of primary keys and their values
     * @return Zend_Db_Table_Rowset
     */
    public function getRow($params)
    {
        $pkeys = $this->info(self::PRIMARY);
        $sel = $this->select();
        foreach ($pkeys as $pk) {
            if (!isset($params[$pk]) || in_array($params[$pk], array(null, ''))) {
                $sel->where($pk . ' IS NULL');
            } else {
                $sel->where($pk . ' = ?', $params[$pk]);
            }
        }

        $row = $this->fetchRow($sel);
        if (empty($row)) {
            throw new Zend_Db_Table_Exception('Query did not return anything [' . $sel . ']');
        }

        return $row;
    }

}
